#include <QMouseEvent>
#include <QApplication>
#include <QDrag>
#include <QMimeData>
#include "CaptureThumbList.h"
#include "ThumbDelegate.h"

CaptureThumbList::CaptureThumbList(QWidget *parent) :
	QListView(parent)
{
	this->setItemDelegate(new ThumbDelegate(this));
}

void CaptureThumbList::mousePressEvent(QMouseEvent *event)
{
	if(event->buttons() == Qt::LeftButton)
		m_ptStart = event->pos();
}

void CaptureThumbList::mouseMoveEvent(QMouseEvent *event)
{
	QModelIndex index = this->indexAt(m_ptStart);

	if(index.isValid() &&
	   event->buttons() == Qt::LeftButton &&
	   (event->pos() - m_ptStart).manhattanLength() > QApplication::startDragDistance())
	{
		QDrag *pDrag = new QDrag(this);
		QMimeData *pMime = new QMimeData;
		QSize size = index.data(Qt::SizeHintRole).toSize();

		pMime->setUrls(QList<QUrl>() << QUrl::fromLocalFile(index.data(Qt::UserRole + 4).toString()));
		pDrag->setMimeData(pMime);
		pDrag->setPixmap(QPixmap::fromImage(index.data(Qt::UserRole + 1).value<QImage>()));
		pDrag->setHotSpot(QPoint(size.width() / 2, size.height() - 5));

		pDrag->exec(Qt::CopyAction);
	}
}
